package clientGUI.userPanels.Controllers;

import java.util.ArrayList;
import java.util.Iterator;

import college.Request;
import college.RequestTreatment;
import college.User;

import college.general;

import clientGUI.CollegeClientGUI;
import clientGUI.Controllers.Controller;
import clientserver.Client;
import clientserver.Packet;
import clientserver.QueryResult;

public class ISEGateController extends Controller

{
	
	public static  enum isRoles {
		
		Evaluator,Chairman,Supervisor,ProcessLeader,Tester
	};
	
	private isRoles role;
	private   ArrayList<String> rids;
	private   ArrayList<String> stages;
	private User connecteduser;
	

	public ISEGateController(Client c){
		client = c;
		rids =new ArrayList<String>();
		stages =new ArrayList<String>();
	}
	

	public isRoles getRole(){
		return role;
	}
	public void setuser( User u)
	{
		connecteduser=u;
	}
	public void setSelectedRole( isRoles role){
		this.role = role;
	}
	
	/**
	 * Function will set the array list rids according to pre-set stages as follows: <p>
	 * @Stages 1, 3 and 10 Are stages awaiting Supervisor input.
	 * @Stages  2 and 4 Are stages awaiting Evaluator input.
	 * @Stages 6 and 8 Are stages awaiting Chairman input.
	 * @Stages 9 and 11 Are stages awaiting ProcessLeader input.
	 * @Stages 14 and 15 Are stages awaiting Tester input.
	 */
	
	public void setRids(){
		
		
			String q = null;
			//String supQuery = null;
		switch (role)
		{ case Supervisor:
			q="SELECT * FROM Requests r WHERE r.supervisor ="+general.addapostrophs(connecteduser.getLogin()); 
			//supQuery = "SELECT * FROM Request_Details rd WHERE (rd.current_stage = '1' OR rd.current_stage = '4') AND rd.responsibleactor ="+general.addapostrophs(connecteduser.getLogin());
			break;
		case Evaluator:
			 q="SELECT * FROM Request_Details rd WHERE (rd.current_stage = '2' OR rd.current_stage = '4') AND rd.responsibleactor ="+general.addapostrophs(connecteduser.getLogin() ); // stage 2  - set deadline, stage 4 write report
		break;
		case Chairman:
			q="SELECT * FROM Request_details rd WHERE  (rd.current_stage = '6' OR rd.current_stage = '8')  AND rd.responsibleactor ="+general.addapostrophs(connecteduser.getLogin());
		   break;
		case ProcessLeader:
			q="SELECT * FROM Request_details rd WHERE  (rd.current_stage = '9' OR rd.current_stage = '11')  AND rd.responsibleactor ="+general.addapostrophs(connecteduser.getLogin());
			   break;
		case Tester: 
			q="SELECT * FROM Request_details rd WHERE  (rd.current_stage = '14' OR rd.current_stage = '15')  AND rd.responsibleactor ="+general.addapostrophs(connecteduser.getLogin());
			   break;
			   default : q="SELECT * FROM Request_Details rd WHERE (rd.current_stage = '2' OR rd.current_stage = '3' OR rd.current_stage = '5') AND rd.responsibleactor ="+general.addapostrophs(connecteduser.getLogin() );
			   break;
		}
			QueryResult qr = null;
			Packet p = new Packet(q);
			try {
		 qr = (QueryResult) client.SendPacketAndWait(p);
		 
		 if(qr.rowsize()==0) 
			{
				rids.removeAll(rids);
				rids.add("None");
			}
				else
			{
						for (int i =0; i < qr.rowsize(); i++)
									{
								   String id=qr.getString("id");
								   int stg = RequestTreatment.getRequestStage(client,Integer.parseInt(id));
									   System.out.println("id="+id);
									   rids.add(id);
										qr.next();
									}
				
			}
			}catch (Exception ex){
				rids.removeAll(rids);
				rids.add("None");
				
			}
	
	
	}
		public ArrayList<String> getRids(){
		return rids;
		}
		
		public boolean getSuspendState(int rid){
			RequestTreatment r = new RequestTreatment(client,rid);
			r.loadfromdb();
			if (r.getCurrentStage() > 20)
				return true;
			return false;
		}
	
		public void suspend(int rid){
			RequestTreatment r = new RequestTreatment(client,rid);
			r.loadfromdb();
			int currState = r.getCurrentStage();
			r.setcurrentstage(currState + 20);
			r.updatestageindb();
		}
		
		public void release(int rid){
			RequestTreatment r = new RequestTreatment(client,rid);
			r.loadfromdb();
			int currState = r.getCurrentStage();
			r.setcurrentstage(currState - 20);
			r.updatestageindb();
		}
			
	}
	
	
	
	
	
	
	
	
	
